<?php
namespace app\backstage\logic;
use think\Db;
class Link
{

    /*
     * 链接全路径获取
     * */
    public function getLinkPathFull($params)
    {
        $Backhaul = \think\facade\App::model('Backhaul');
        $info = $Backhaul->getOne(['id'=>$params['id']],'channel_id,advert_media_id');

        if(empty($info))
        {
            return ['code'=>101,'msg'=>'回传记录ID无效'];
        }

        $quick_mp = Db::table('quick_app_mp_manager')->where('uid',$info['channel_id'])->field('quick_id')->findOrEmpty();
        if(empty($quick_mp))
        {
            return ['code'=>101,'msg'=>'该投放号未绑定App，请设置'];
        }

        $infoQuick = Db::table('quick_app')->where('id',$quick_mp['quick_id'])->field('package_name')->findOrEmpty();
        if(empty($infoQuick))
        {
            return ['code'=>101,'msg'=>'该投放号未绑定App，请设置'];
        }

        $mediaAccounts = $this->getAdAccount($info['advert_media_id'], $info['channel_id']);
        $mp = getLinkHapMacroParameters($info['advert_media_id']);

        if (strlen($mp[0]) < 1) {
            $data['hap'] = "";
            $data['hap2'] = "";
        } else {
            $data['hap'] = ""; //hap://app/{$infoQuick['package_name']}/pages/read?channel_id={$info['channel_id']}" . $mp[0];
            if (isset($mp[1])) {
                $data['hap2'] = "";//"hap://app/{$infoQuick['package_name']}/pages/read?channel_id={$info['channel_id']}" . $mp[1];
            } else {
                $data['hap2'] = '';
            }
        }
        $data['monitor'] = getLinkMacroParameters($info['advert_media_id'], $info['channel_id'], 0);
        //百度添加账户后缀
        if ($info['advert_media_id'] == 4){
            if (!$mediaAccounts){
                $data['monitor'] = '';
                $data['hap'] = "";
                $data['hap2'] = "";
            }else{
                $data['monitor'] = $data['monitor'] . "&mysign={$mediaAccounts[0]['mysign']}";
            }
        }
        //vivo添加账户后缀
        if ($info['advert_media_id'] == 6){
            if (!$mediaAccounts){
                $data['monitor'] = '';
                $data['hap'] = "";
                $data['hap2'] = "";
            }else{
                $data['monitor'] = $data['monitor'] . "";
            }
        }

        if ($info['advert_media_id'] == 3) {
            $data['h5Jump'] = "";//config('api_domain') . "/link/open?channel_id={$info['channel_id']}" . getH5JumpLink($info['advert_media_id'])[0];
            $data['h5Jump2'] = "";//"http://ldy.yxwlaqq.cn/jump_content?token={$info['channel_id']}" . getH5Jump2Link($info['advert_media_id'])[0];
        }

        $data['link_id'] = 0;
        $data['id'] = $params['id'];
        $data['media_id'] = $info['advert_media_id'];
        $data['mediaAccounts'] = $mediaAccounts;
        return ['code'=>100,'msg'=>'success','data'=>$data];
    }

    /**
     * 获取媒体账户
     * @param $mediaId
     * @param $channelId
     * @return array|\PDOStatement|string|\think\Collection|\think\model\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function getAdAccount($mediaId, $channelId)
    {
        $mediaAccounts = [];
        $acWhere[] = ['channel_id', '=', $channelId];
        $acWhere[] = ['status', '=', 1];
        if ($mediaId == 4){
            $table = "quick_ad_account_baidu";
        }elseif ($mediaId == 6){
            $table = "quick_ad_account_vivo";
        }else{
            $table = "";
        }
        if ($table){
            $mediaAccounts = Db::table($table)->where($acWhere)->select();
        }
        foreach ($mediaAccounts as $k => $v){
            $mediaAccounts[$k]['akey_text'] = '';
            if ($k === 0){
                $mediaAccounts[$k]['selected'] = 1;
            }else{
                $mediaAccounts[$k]['selected'] = 0;
            }
            if ($mediaId == 4) {
                if ($v['isact'] == 0) {
                    $mediaAccounts[$k]['akey_text'] = '【未激活】';
                }
            }
        }
        return $mediaAccounts;
    }

    /*获取所有推广链接*/
    public function getAllList($where)
    {
        $field = 'id,name';
        $orderBy = ['id' => 'DESC'];
        $Link = \think\facade\App::model('Link');
        $list = $Link->getAll($where, $field, $orderBy);
        return $list;
    }
}